<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="4">
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!--角色列表-->
      <el-table :data="rolesList" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['vcenter','bdbottom',i1===0 ? 'bdtop' : '']" v-for="(item1,i1) in scope.row.children"
                    :key="item1.id">
              <!--这里是一级-->
              <el-col :span="5">
                <el-tag closable
                        @close="removewRightById(scope.row,item1.id)">{{item1.authName}}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>

              <!--这里是二三级-->
              <el-col :span="19">
                <el-row :class="['vcenter',i2===0 ? '' : 'bdtop']" v-for="(item2,i2) in item1.children" :key="item2.id">
                  <!--这里是二级-->
                  <el-col :span="6">
                    <el-tag type="success" closable
                            @close="removewRightById(scope.row,item2.id)">{{item2.authName}}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!--这里是三级-->
                  <el-col :span="18">
                    <el-tag type="warning" v-for="(item3,i3) in item2.children" :key="item3.id" closable
                            @close="removewRightById(scope.row,item3.id)">{{item3.authName}} {{i3}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="##" type="index"></el-table-column>
        <el-table-column label="角色" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
            <el-tooltip class="item" effect="dark" content="权限分配" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini"
                         @click="showSetRightDialog(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--权限分配-->
    <el-dialog
      title="权限列表"
      :visible.sync="showSetRightDialogVisible"
      width="60%">
      <!--      树 控件-->
      <el-tree :data="rightsList" :props="treeProps" show-checkbox node-key="id" default-expand-all
               :default-checked-keys="defKeys" ref="treeRef"></el-tree>
      <span slot="footer" class="dialog-footer">
    <el-button @click="showSetRightDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="allotRights()">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        rolesList: [], // 角色
        rightsList: [], // 权限
        treeProps: {
          label: 'authName',
          children: 'children'
        },
        defKeys: [], // 默认选中的节点
        roleid: 0,
        showSetRightDialogVisible: false // 控制权限分配
      }
    },
    created () {
      this.getRolesList()
    },
    methods: {
      async getRolesList () {
        const { data: res } = await this.$http.get('roles')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.rolesList = res.data
      },
      // 根据ID删除 对应的权限
      async removewRightById (role, rightId) {
        console.log(role, rightId)
        // 弹窗
        const confirmResult = await this.$confirm('此操作将删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(msg => {
          return msg
        }).catch(err => {
          return err
        })

        if (confirmResult !== 'confirm') {
          return this.$message({
            type: 'info',
            message: '已取消删除'
          })
        }

        const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
        if (res.meta.status !== 200) {
          this.$message.error(res.meta.msg)
        } else {
          this.$message.success(res.meta.msg)
          // this.getRolesList()
          role.children = res.data
        }
      },
      // 展示分配权限
      async showSetRightDialog (row) {
        this.roleid = row.id
        const { data: res } = await this.$http.get('rights/tree')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.rightsList = res.data
        this.defKeys = []
        this.getLeafKeys(row, this.defKeys)
        this.showSetRightDialogVisible = true
        // 获取所有权限
      },
      // 递归获取权限ID
      getLeafKeys (node, arr) {
        if (!node.children) {
          return arr.push(node.id)
        }
        node.children.forEach(item => this.getLeafKeys(item, arr))
      },
      // 保存权限
      async allotRights () {
        const keys = [
          ...this.$refs.treeRef.getCheckedKeys(),
          ...this.$refs.treeRef.getHalfCheckedKeys()
        ]
        console.log(keys)
        const idStrs = keys.join(',')
        const { data: res } = await this.$http.post(`roles/${this.roleid}/rights`, { rids: idStrs })
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.showSetRightDialogVisible = false
        this.getRolesList()
      }
    }
  }
</script>

<style lang="less" scoped>
  .el-table {
    margin: 12px;
    font-size: 12px;
  }
  .el-tag {
    margin: 7px;
  }

  .vcenter {
    display: flex;
    align-items: center;
  }

  .bdtop {
    border-top: 1px solid #eee;
  }

  .bdbottom {
    border-bottom: 1px solid #eee;
  }
</style>
